package cardgame.demo;


import org.springframework.util.CollectionUtils;

import java.util.*;

public class GameStarter {
    public static final String PLAYER_STATUS = "playerStatus";
    private static final String SEAT_NUM = "seatNum";
    private static Random random = new Random(System.currentTimeMillis());
    private static HashMap<String, HashMap<String, Object>> gameCommonStatusMap = new HashMap<>();
    private static final String[] IDENTITY_CARD_ARRAY = new String[]{"潜伏","军情","特工"};
    private String roomName;
    public static void main(String[] args) {
        GameStarter gameStarter = new GameStarter("房间1");
        gameStarter.addPlayer("playerId1");
        gameStarter.addPlayer("playerId2");
        gameStarter.addPlayer("playerId3");
        gameStarter.startGame();
    }

    private void addPlayer(String playerId) {
        HashMap<String, Object> player = new HashMap<>();
        player.put("id", playerId);
        getPlayerStatusMap().put(playerId, player);
    }

    public void startGame(){
        randomSeatNumber();
        randomIdentityCard();
        int playerSize = getSeatNumMap().keySet().size();
        HashMap<String, Object> seatNumMap = getSeatNumMap();
        for(int i = 1; i <= playerSize; i++){
            System.out.println("seat["+i+"]=["+seatNumMap.get(String.valueOf(i))+"]");
        }
        for(Map.Entry<String, Object> entry: getPlayerStatusMap().entrySet()){
            System.out.println(entry.getKey()+"="+entry.getValue());
        }

    }

    private void randomSeatNumber() {
        Set<String> playerIdKeySet = getPlayerStatusMap().keySet();
        int size = playerIdKeySet.size();
        Integer[] seatNumArray = new Integer[size];
        for(int i = 0; i < size; i++){
            seatNumArray[i] = i+1;
        }
        shuffleArray(seatNumArray);
        int index = 0;
        for(String playerId:playerIdKeySet){
            HashMap<String, Object> playerHashMap = (HashMap<String, Object>)getPlayerStatusMap().get(playerId);
            int seatNum = seatNumArray[index++];
            String seatNumKey = String.valueOf(seatNum);
            playerHashMap.put("seatNum", seatNum);
            getSeatNumMap().put(seatNumKey, playerId);
        }
    }

    public GameStarter(String roomName) {
        this.roomName = roomName;
        initRoom();
    }

    private HashMap<String, Object> getRoomStatusMap(){
        return gameCommonStatusMap.get(roomName);
    }

    private HashMap<String, Object> getSeatNumMap(){
        return (HashMap<String, Object>) getRoomStatusMap().get(SEAT_NUM);
    }

    private HashMap<String, Object> getPlayerStatusMap(){
        return (HashMap<String, Object>) getRoomStatusMap().get(PLAYER_STATUS);
    }

    private void initRoom() {
        gameCommonStatusMap.put(roomName, new HashMap<String, Object>());
        getRoomStatusMap().put(PLAYER_STATUS, new HashMap<String,Object>());
        getRoomStatusMap().put(SEAT_NUM, new HashMap<String,Object>());
    }

    static HashMap<Integer, String[]> identityCardMap = new HashMap<>();
    static {
        identityCardMap.put(3, new String[]{"潜伏","军情","特工"});
    }


    private void randomIdentityCard() {
        Set<String> seatNumKeySet = getSeatNumMap().keySet();
        int size = seatNumKeySet.size();
        List<String> list = shuffleAndCopyArray(identityCardMap.get(size));
        for(int i = 0; i < size; i++){
           String playerId= (String) getSeatNumMap().get(String.valueOf(i+1));
            HashMap<String,Object> playerHashMap = (HashMap<String, Object>) getPlayerStatusMap().get(playerId);
            playerHashMap.put("identityCard", list.get(i));
        }
    }

//    public static <T> T[]  shuffleCopyArray(T[] array){
//        if(array == null){
//            throw new NullPointerException("array must be not null");
//        }
//        CollectionUtils.arrayToList()
//        for(int lastIndex = array.length-1; lastIndex > 0; lastIndex--){
//            int randomIndex = random.nextInt(lastIndex);
//            if(randomIndex != lastIndex){
//                T temp = array[lastIndex];
//                array[lastIndex] = array[randomIndex];
//                array[randomIndex] = temp;
//            }
//        }
//    }

    public static <T> void shuffleArray(T[] array){
        if(array == null){
            throw new NullPointerException("array must be not null");
        }
        for(int lastIndex = array.length-1; lastIndex > 0; lastIndex--){
            int randomIndex = random.nextInt(lastIndex+1);
            if(randomIndex != lastIndex){
                T temp = array[lastIndex];
                array[lastIndex] = array[randomIndex];
                array[randomIndex] = temp;
            }
        }
    }

    public static <T> List<T> shuffleAndCopyArray(T[] array){
        if(array == null){
            throw new NullPointerException("array must be not null");
        }
        List<T> list = new ArrayList<T>(array.length);
        Collections.addAll(list, array);
//        list.addAll((List<T>)CollectionUtils.arrayToList(array));
        shuffleArray(list);
        return list;
    }

    public static <T> void shuffleArray(List<T> array){
        if(array == null){
            throw new NullPointerException("array must be not null");
        }
        for(int lastIndex = array.size()-1; lastIndex > 0; lastIndex--){
            int randomIndex = random.nextInt(lastIndex+1);
            if(randomIndex != lastIndex){
                T temp = array.get(lastIndex);
                array.set(lastIndex, array.get(randomIndex));
                array.set(randomIndex, temp);
            }
        }
    }
}
